Let's learn how to draw some pictures.
Our metaphor here is that there's a virtual turtle.
Starts out in the middle of the screen.
In the turtle's tail, there's a pen.
So, when the turtle moves around,
it leaves a drawing on the screen.
In this code, we've created a turtle named Alex,
and we're telling it to move forward and turn left and go forward again.
Let's see what its picture looks like.
So, in this case,
our virtual turtle is really represented by just an arrow.
Doesn't even look like a turtle.
But he started over here,
moved forward by 150 pixels.
That's what happened on this line of code,
told him to start here and go forward by 150 pixels.
He then turned left by 90 degrees.
That's what the line five told him to do.
Then, he moved forward by 75.
Let's go through in a little more detail what each of these lines of code does.
The first line just says to make our turtle commands be available.
The second line says, make a screen.
So, that's what line two does.
Then, any turtle things that we have in
the rest of the program will happen on that screen.
Line three makes a turtle.
That's what creates our little turtle guy,
and it assigns that turtle object to the variable called Alex.
On line four, we are invoking a method, the forward method.
So, this is our hidden agenda in this lesson,
is to teach you about objects in this dot notation.
So, Alex is the name of the turtle.
If you have an object and then a dot,
it will look up attributes or methods of that object.
So, alex.forward says, find the forward method,
which is just like the functions that you've seen before. It takes an action.
In this case, it says,
take the forward action on Alex,
and we pass in a value 150.
That says how much Alex should move forward.
So, we have object.method,
and then, in parentheses,
we pass a value.
So, on line five,
we're telling Alex to turn left by 90 degrees.
Line six says, move forward by 75 degrees.
Because Alex has a pen in his tail,
we get to see the lines that show where he went.
So, little vocabulary we've gotten here,
we've gotten the idea of a instance.
Alex is an instance of the turtle class,
wn is an instance of the screen class.
So, turtle is a class,
Alex is an instance of that class.
Screen is a class,
wn is an instance of that class.
We've gotten the idea of a method,
like forward or left.
Now, in addition to these things,
we can also set and read attributes.
In this turtle drawing,
we don't really need to do it,
but I want to show you how setting attributes works for object instances.
So, Alex is an instance,
and if I want to set any attribute,
let's say, Alex's salary,
because he should really be paid a lot for doing all of this drawing work for us,
and we'll set his salary,
let's say, to $50,000.
If I do that, Alex will store in its salary attribute the value 50,000,
and I can then refer to it just like we would other variables.
So, Alex refers to that instance,
and we can print out his salary.
As before, when we print things,
they're going to show up in our output window.
So, this is going to redraw that whole thing,
and at the end of it, it's going to print out the 50,000.
That's because we printed it here.
We're not going to do much with these attributes of
instances for a while until much later in this course sequence,
but we wanted to give you a hint of it before we went too far with Python.
I want to make a little more interesting picture.
Let's see if we can finish making a rectangle here.
So, instead of doing this stuff with Alex's salary,
let me get rid of that, and let me have Alex do a little bit more.
I'm going to have him turn to the left by 90 degrees again,
and what that should do is turn him that way so that when he goes forward,
he'll be going in that direction.
Now, how far should I make him go if I want it to be the same distance as this one was?
Well, that one came from here,
so I have to have him go forward by 150.
Let's see what happens when we run that.
It's always a good idea to run things when you write some code.
Great. That seems to have worked.
Let's have him turn and finish the rectangle.
This time, I have to go just 75, the shorter length.
Sure enough, we've got a rectangle there.